from django.db import models
from goods.models import *
from users.models import *


# 优惠类型
class CouponType(models.Model):
    type_name = models.CharField("优惠券类型名称", max_length=30)

    def __str__(self):
        return self.type_name

    class Meta:
        db_table = "coupon_type"
        verbose_name_plural = "优惠券类型"


# 优惠券
class Coupon(models.Model):
    name = models.CharField("优惠券名称", max_length=100)
    brief = models.TextField("优惠券介绍", default="")
    coupon_type = models.ForeignKey(CouponType, on_delete=models.CASCADE ,verbose_name="优惠券类型",
                                    # 前端添加可以为空   超级管理员添加可以为空
                                    null=True, blank=True)
    good = models.ForeignKey(SKU, on_delete=models.CASCADE, verbose_name="可用的商品")
    coupon_num = models.IntegerField("数量", default=0)
    start_time = models.DateTimeField("开始使用时间")
    end_time = models.DateTimeField("结束使用时间")
    start_get_time = models.DateTimeField("开始领取时间")
    end_get_time = models.DateTimeField("领取结束时间")
    remark = models.CharField("备注信息", max_length=300)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'coupon'
        verbose_name_plural = "优惠券"


# 优惠券 --类型 --具体的值
class CouponTypeValue(models.Model):
    coupon = models.ForeignKey(Coupon, on_delete=models.CASCADE, verbose_name="优惠券")
    coupon_type = models.ForeignKey(CouponType, on_delete=models.CASCADE, verbose_name="优惠券类型")
    value = models.IntegerField("类型具体的值", default=0)

    def __str__(self):
        return "优惠券%s:类型%s:值%s"%(self.coupon.name,self.coupon_type.type_name,self.value)

    class Meta:
        db_table = 'coupon_value'
        verbose_name_plural = "优惠券的面值"


# 用户领券
class UserGetCoupon(models.Model):
    COUPON_STATUS = (
        (0, "未使用"),
        (1, "已使用"),
        (2, "已过期")
    )
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="用户")
    coupon = models.ForeignKey(Coupon, on_delete=models.CASCADE, verbose_name="优惠券")
    status = models.SmallIntegerField("优惠券的状态", choices=COUPON_STATUS, default=0)
    get_time = models.DateTimeField("领取时间", auto_now_add=True)
    use_time = models.DateTimeField("使用时间", null=True, blank=True)

    def __str__(self):                                                          # 显示文本内容
        return "用户%s-%s-%s"%(self.user.username, self.coupon.name,self.get_status_display())

    class Meta:
        db_table = "user_get_coupon"
        verbose_name_plural = "用户领券"














